package com.tinyideatech.admin.controller.mall;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.MallCarousel;
import com.tinyideatech.system.service.IMallCarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-08-12
 */

@Controller
@RequestMapping("/mall/carousel")
public class MallCarouselController extends BaseController {

    @Autowired
    private IMallCarouselService mallCarouselService;

    @GetMapping()
    public String list() {
        return "mall/carousel/list";
    }

    @GetMapping("/add")
    public String add() {
        return "mall/carousel/add";
    }

    @GetMapping("/edit/{id}")
    public String mallCarouselEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        MallCarousel carousel = mallCarouselService.getById(id);
        map.addAttribute("carousel", carousel);
        return "mall/carousel/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(MallCarousel mallCarousel) {
        QueryWrapper<MallCarousel> qw = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(mallCarousel.getCarouselName())){
            qw.like("carousel_name", mallCarousel.getCarouselName());
        }
        if(StringUtils.isNotEmpty(mallCarousel.getGoodsId())){
            qw.like("goods_id", mallCarousel.getGoodsId());
        }
        IPage<MallCarousel> page = startPage();
        IPage<MallCarousel> mallCarouselList = mallCarouselService.page(page, qw);
        return page(mallCarouselList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            MallCarousel sysRole
    ) {
        mallCarouselService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ){
        Long[] mallCarouselIds = Convert.toLongArray(ids);
        mallCarouselService.removeByIds(Arrays.asList(mallCarouselIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(MallCarousel mallCarousel) {
        mallCarouselService.changeStatus(mallCarousel);
        return success();
    }

}
